package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void upload(List<OrderSetting> orderSettingList) {
        if(orderSettingList!=null&&orderSettingList.size()>0) {
            for (OrderSetting orderSetting : orderSettingList) {
                Date date = orderSetting.getOrderDate();
                Long count = orderSettingDao.findOrderSettingByDate(date);
                if(count>0){
                    orderSettingDao.upload(orderSetting);
                }else {
                    orderSettingDao.add(orderSetting);
                }

            }
        }
    }

    @Override
    public List<Map> findPage(String stringDate) {
        String startDate=stringDate+"-01";
        String endDate=stringDate+"-31";
        List<OrderSetting>orderSettingList=orderSettingDao.findPage(startDate,endDate);
        List<Map> maps = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Object> map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            maps.add(map);
        }
        return maps;
    }

    @Override
    public void setting(OrderSetting orderSetting) {
        Date date = orderSetting.getOrderDate();
        long date1 = orderSettingDao.findOrderSettingByDate(date);
        if(date1>0){
            orderSettingDao.upload(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }

    @Override
    public List<OrderSetting> findAllOrderSetting() {
        List<OrderSetting> orderSettingList=orderSettingDao.findAllOrderSetting();
        return orderSettingList;
    }

    @Override
    public OrderSetting findOrderSettingByDate(Date date) {
        OrderSetting orderSetting=orderSettingDao.findOrderSettingInfoByDate(date);
        return orderSetting;
    }
}
